#include "header.h"
int main(int argc, char *argv[])
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        std::cout << "socket fail" << std::endl;
    }
    sockaddr_in client;
    client.sin_family = AF_INET;
    client.sin_addr.s_addr = inet_addr(argv[1]);
    client.sin_port = htons(atoi(argv[2]));
    socklen_t len = sizeof(client);
    int cret = connect(sockfd, (sockaddr *)(&client), len);
    if (cret < 0)
        std::cout << "connect fail" << std::endl;
    while (true)
    {
        int input;
        char buffer[SIZE_BUFFER];
        std::string tem;
        // 接受菜单
        ssize_t s = recv(sockfd, buffer, SIZE_BUFFER - 1, 0);
        if (s > 0)
            buffer[s] = '\0';
        std::cout << buffer << std::endl;
        memset(buffer, 0, SIZE_BUFFER);
        std::cout << "请选择" << std::endl;
        std::cin >> input;
        tem = std::to_string(input).c_str();
        // 发送选择
        send(sockfd, tem.c_str(), tem.size(), 0);
        tem.clear();
        // 读取棋盘
        s = recv(sockfd, buffer, SIZE_BUFFER - 1, 0);
        if (s > 0)
            buffer[s] = '\0';
        tem = buffer;
        std::cout << tem << std::endl;
        memset(buffer, 0, SIZE_BUFFER);
        while (true)
        {
            std::cout << "请选择要开的地方" << std::endl;
            int x;
            int y;
            std::cout << "x:";
            std::cin >> x;
            std::cout << "y:";
            std::cin >> y;
            tem = std::to_string(x);
            tem += "\n";
            tem += std::to_string(y);
            // 发送选择
            send(sockfd, tem.c_str(), tem.size(), 0);
            //读取棋盘
            s = recv(sockfd, buffer, SIZE_BUFFER - 1, 0);
            if (s > 0)
                buffer[s] = '\0';
            tem = buffer;
            std::cout << tem << std::endl;
            memset(buffer, 0, SIZE_BUFFER);
        }
    }
    return 0;
}